
package database;

import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.SQLClientInfoException;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;


public class DBConnection implements Connection
{
    private DBConnectionPool pool;

    private Connection connection;

    private boolean isInUse;

    private long timestamp;

    public DBConnection( Connection connection , DBConnectionPool pool  )
    {
        this.connection = connection;
        this.pool = pool;
        this.isInUse = false;
        this.timestamp = 0;
    }

    public synchronized boolean lease()
    {
        if ( isInUse )
        {
            return false;
        }
        else
        {
            isInUse = true ;
            
            timestamp = System.currentTimeMillis() ;
            
            return true ;
        }
    }

    public boolean validate()
    {
        try
        {
            connection.getMetaData();
        }
        catch ( Exception e )
        {
            return false;
        }
        
        return true;
    }

    public boolean inUse()
    {
        return isInUse;
    }

    public long getLastUse()
    {
        return timestamp;
    }

    public void close() throws SQLException
    {
        pool.returnConnection( this );
    }

    protected void expireLease()
    {
        isInUse = false;
    }

    protected Connection getConnection()
    {
        return connection;
    }

    public PreparedStatement prepareStatement( String sql ) throws SQLException
    {
        return connection.prepareStatement( sql );
    }

    public CallableStatement prepareCall( String sql ) throws SQLException
    {
        return connection.prepareCall( sql );
    }

    public Statement createStatement() throws SQLException
    {
        return connection.createStatement();
    }

    public String nativeSQL( String sql ) throws SQLException
    {
        return connection.nativeSQL( sql );
    }

    public void setAutoCommit( boolean autoCommit ) throws SQLException
    {
        connection.setAutoCommit( autoCommit );
    }

    public boolean getAutoCommit() throws SQLException
    {
        return connection.getAutoCommit();
    }

    public void commit() throws SQLException
    {
        connection.commit();
    }

    public void rollback() throws SQLException
    {
        connection.rollback();
    }

    public boolean isClosed() throws SQLException
    {
        return connection.isClosed();
    }

    public DatabaseMetaData getMetaData() throws SQLException
    {
        return connection.getMetaData();
    }

    public void setReadOnly( boolean readOnly ) throws SQLException
    {
        connection.setReadOnly( readOnly );
    }

    public boolean isReadOnly() throws SQLException
    {
        return connection.isReadOnly();
    }

    public void setCatalog( String catalog ) throws SQLException
    {
        connection.setCatalog( catalog );
    }

    public String getCatalog() throws SQLException
    {
        return connection.getCatalog();
    }

    public void setTransactionIsolation( int level ) throws SQLException
    {
        connection.setTransactionIsolation( level );
    }

    public int getTransactionIsolation() throws SQLException
    {
        return connection.getTransactionIsolation();
    }

    public SQLWarning getWarnings() throws SQLException
    {
        return connection.getWarnings();
    }

    public void clearWarnings() throws SQLException
    {
        connection.clearWarnings();
    }

    /*
     * Unimplemented Methods ...
     */
    
    public Statement createStatement( int resultSetType , int resultSetConcurrency ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PreparedStatement prepareStatement( String sql , int resultSetType , int resultSetConcurrency ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public CallableStatement prepareCall( String sql , int resultSetType , int resultSetConcurrency ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Map<String , Class<?>> getTypeMap() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public void setTypeMap( Map<String , Class<?>> map ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public void setHoldability( int holdability ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public int getHoldability() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Savepoint setSavepoint() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Savepoint setSavepoint( String name ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public void rollback( Savepoint savepoint ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public void releaseSavepoint( Savepoint savepoint ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Statement createStatement( int resultSetType , int resultSetConcurrency , int resultSetHoldability ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PreparedStatement prepareStatement( String sql , int resultSetType , int resultSetConcurrency , int resultSetHoldability ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public CallableStatement prepareCall( String sql , int resultSetType , int resultSetConcurrency , int resultSetHoldability ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PreparedStatement prepareStatement( String sql , int autoGeneratedKeys ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PreparedStatement prepareStatement( String sql , int[] columnIndexes ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PreparedStatement prepareStatement( String sql , String[] columnNames ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Clob createClob() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Blob createBlob() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public NClob createNClob() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public SQLXML createSQLXML() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public boolean isValid( int timeout ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public void setClientInfo( String name , String value ) throws SQLClientInfoException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public void setClientInfo( Properties properties ) throws SQLClientInfoException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public String getClientInfo( String name ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Properties getClientInfo() throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Array createArrayOf( String typeName , Object[] elements ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public Struct createStruct( String typeName , Object[] attributes ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public <T> T unwrap( Class<T> iface ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public boolean isWrapperFor( Class<?> iface ) throws SQLException
    {
        throw new UnsupportedOperationException( "Not supported yet." );
    }
}
